<div id="app">
    <section class="content-header">
        <h1>
            <small>优惠券添加</small>
        </h1>

    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-info">
                    <div class="box-header with-border">


                        <div class="box-tools">
                            <div class="btn-group pull-right" style="margin-right: 5px">
                                <a href="/admin/coupon/index" class="btn btn-sm btn-default" title="优惠券列表"><i class="fa fa-list"></i><span class="hidden-xs"> 优惠券列表 </span></a>
                            </div>
                        </div>

  <span>
    <a class="btn btn-sm btn-primary grid-refresh" title="刷新"><i class="fa fa-refresh"></i><span class="hidden-xs"> 刷新</span></a>
    <a class="btn btn-sm btn-primary" onclick="javascript:history.go(-1);" title="返回"> <span class="hidden-xs"> 返回</span></a>
  </span>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form id="addForm"   method="post" accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data" pjax-container>
                        <div class="box-body">
                            <div class="fields-group">

                                <div class="form-group  ">
                                    <label for="cou_name" class="col-sm-2  control-label">优惠券名称</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-pencil fa-fw"></i></span>
                                            <input type="text" id="cou_name" name="cou_name" value="" class="form-control cou_name"  />
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cou_effect" class="col-sm-2  control-label">优惠券标题</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-pencil fa-fw"></i></span>
                                            <input type="text" id="cou_effect" name="cou_effect" value="" class="form-control cou_effect"  />
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cou_describe" class="col-sm-2  control-label">优惠券描述</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-pencil fa-fw"></i></span>
                                            <input type="text" id="cou_describe" name="cou_describe" value="" class="form-control cou_describe"  />
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="is_breaks" class="col-sm-2  control-label">优惠券类型</label>
                                    <div class="col-sm-8">
                                        <label class="radio-inline">
                                            <input type="radio" name="is_breaks" value="1" class="minimal is_breaks" checked  />&nbsp;满减卷
                                        </label>
                                        <label class="radio-inline">
                                            <input type="radio" name="is_breaks" value="2" class="minimal is_breaks"   />&nbsp;折扣卷&nbsp;&nbsp;
                                        </label>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cou_sum_price" class="col-sm-2  control-label">满减</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-pencil fa-fw"></i></span>
                                            <input type="text" id="cou_sum_price" name="cou_sum_price" value="" class="form-control cou_sum_price"  />
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cou_price" class="col-sm-2  control-label">满减金额</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-pencil fa-fw"></i></span>
                                            <input type="text" id="cou_price" name="cou_price" value="" class="form-control cou_price"  />
                                        </div>
                                    </div>
                                </div>




                                <div class="form-group  ">
                                    <label for="cou_start_time" class="col-sm-2  control-label">活动时间</label>
                                    <div class="col-sm-8">
                                        <div class="row" style="width: 390px">
                                            <div class="col-lg-6">
                                                <div class="input-group">
                                                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                                    <input type="text" name="cou_start_time" value="" class="form-control cou_start_time" style="width: 160px"  />
                                                </div>
                                            </div>
                                            <div class="col-lg-6">
                                                <div class="input-group">
                                                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                                    <input type="text" name="cou_end_time" value="" class="form-control cou_end_time" style="width: 160px"  />
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cou_expiration" class="col-sm-2  control-label">有效天数</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <input   style="width: 100px;display: inline-block" type="text" id="cou_expiration" name="cou_expiration" value="8" class="form-control cou_expiration"   />
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="is_breaks" class="col-sm-2  control-label">是否启用</label>
                                    <div class="col-sm-8">
                                        <label class="radio-inline">
                                            <input type="radio" name="cou_status" value="1" class="minimal cou_status" checked  />&nbsp;启用
                                        </label>
                                        <label class="radio-inline">
                                            <input type="radio" name="cou_status" value="2" class="minimal cou_status"   />&nbsp;不启用&nbsp;&nbsp;
                                        </label>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cha_name" class="col-sm-2  control-label">渠道选择</label>
                                    <div class="col-sm-8">
                                        <select id="cha_name" onchange="getCate(this)" class="form-control cha_name" style="width: 100%;" name="cha_name" data-value="" >
                                            <option value=""></option>
                                            @if($channel)
                                                @foreach($channel as $k=>$v)
                                                    <option value="{{$k}}" >{{$v}}</option>
                                                @endforeach
                                            @endif
                                        </select>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cate_id" class="col-sm-2  control-label">适用分类</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <select style="width:200px;" id="cate_id" size="20" name="cate_id[]" multiple="multiple"  >
                                                <option value="0">不选</option>
                                                <option value="all">全部</option>
                                            </select>

                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="goods_id" class="col-sm-2  control-label">适用商品</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <textarea name="goods_id" style="width:300px;height:200px;" placeholder="商品id用英文逗号间隔"></textarea>
                                            {{--<input type="text" id="goods_id" name="goods_id" value="" class="form-control goods_id"  />--}}
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cou_every_count" class="col-sm-2  control-label">默认拥有数</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <input   style="width: 100px;display: inline-block" type="text" id="cou_every_count" name="cou_every_count" value="1" class="form-control cou_every_count"   />

                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cou_type" class="col-sm-2  control-label">是否收藏后才能领取</label>
                                    <div class="col-sm-8">
                                        <label class="radio-inline">
                                            <input type="radio" name="cou_type" value="0" class="minimal cou_type" checked  />&nbsp;否
                                        </label>
                                        <label class="radio-inline">
                                            <input type="radio" name="cou_type" value="1" class="minimal cou_type"   />&nbsp;是&nbsp;&nbsp;
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->

                        <div class="box-footer">
                            @csrf
                            <div class="col-md-2">
                            </div>
                            <div class="col-md-8">
                                <div class="btn-group pull-right">
                                    <button type="button" onclick="tijiao()" class="btn btn-primary">提交</button>
                                </div>

                                <div class="btn-group pull-left">
                                    <button type="reset" class="btn btn-warning">重置</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>

            </div></div>
    </section>
</div>
<script data-exec-on-popstate>
    $(function () {
        $('.cou_sum_price').inputmask({"alias":"currency","radixPoint":".","prefix":"","removeMaskOnSubmit":true});
        $('.cou_price').inputmask({"alias":"currency","radixPoint":".","prefix":"","removeMaskOnSubmit":true});

        $('.cou_start_time').datetimepicker({"format":"YYYY-MM-DD HH:mm:ss","locale":"zh-CN"});
        $('.cou_end_time').datetimepicker({"format":"YYYY-MM-DD HH:mm:ss","locale":"zh-CN","useCurrent":false});
        $(".cou_start_time").on("dp.change", function (e) {
            $('.cou_end_time').data("DateTimePicker").minDate(e.date);
        });
        $(".cou_end_time").on("dp.change", function (e) {
            $('.cou_start_time').data("DateTimePicker").maxDate(e.date);
        });

        $('.cou_expiration:not(.initialized)')
            .addClass('initialized')
            .bootstrapNumber({
                upClass: 'success',
                downClass: 'primary',
                center: true
            });

        $('.cou_every_count:not(.initialized)')
            .addClass('initialized')
            .bootstrapNumber({
                upClass: 'success',
                downClass: 'primary',
                center: true
            });


        //刷新
        $('.grid-refresh').on('click', function() {
            $.pjax.reload('#pjax-container');
            toastr.success('刷新成功 !');
        });
    });


    function tijiao(){
        var data=new FormData($("#addForm")[0]);
        $.ajax({
            url:"/admin/coupon/add",
            type:'post',
            data:data,
            dataType:'json',
            processData: false,
            contentType: false,
            success:function(m){
                if(m.status==1){
                    toastr.success(m.message);
                    window.location.href='/admin/coupon/index';
                }else{
                    swal(m.message, '', 'error');
                }
            }
        });
    }

    function getCate(e) {
        var name=$(e).val();
        $("#cate_id").html("<option value='0'>不选</option>");
        if(name){
            $.ajax({
                url:"/admin/coupon/getCategory",
                type:'post',
                data:{cha_name:name,_token:LA.token},
                dataType:'json',
                success:function(m){
                    if(m.status==1){
                        $("#cate_id").html(m.data.html);
                    }else{

                        $("#cate_id").html("<option value='0'>不选</option><option value='all'>全部</option>");
                    }
                }
            });
        }
    }

    function zhuijia() {
        var zhi=$("#category").val();
        if(zhi){
            var cate=$("#cate_id").text().split(',');
            console.log($("#cate_id").html());
            console.log(cate);
            var cate = cate.concat(zhi);
            var ar=[];
            for(var i=0;i<cate.length;i++){
                if(cate[i]!='' && ar.indexOf(cate[i])==-1){
                    ar.push(cate[i]);
                }
            }
            $("#category").val(ar);

            console.log(ar);

            $("#cate_id").text(ar.join());
        }


    }
</script>